package com.maxie.demo.lottery;

import com.maxie.demo.lottery.entity.LotteryResult;
import com.maxie.demo.lottery.event.EventManager;
import com.maxie.demo.lottery.event.listener.EmailEventListener;
import com.maxie.demo.lottery.event.listener.PhoneMessageEventListener;

/**
 * @description: 抽象抽奖服务
 * 为什么✅抽象类：
 *
 */
public abstract class LotteryService {


    private EventManager eventManager;

    public LotteryService() {
        System.out.println("LotteryService初始化抽奖服务");
        this.eventManager = new EventManager(EventManager.EventType.MESSAGE, EventManager.EventType.EMAIL);
        eventManager.subscribe(EventManager.EventType.MESSAGE, new PhoneMessageEventListener());
        eventManager.subscribe(EventManager.EventType.EMAIL, new EmailEventListener());
    }

    public void doDraw(String uId) {
        // 1. 模拟抽奖
        LotteryResult result = draw(uId);
        // 2. 通知结果
        if (result.getFlag() == 1) {
            eventManager.notify(EventManager.EventType.MESSAGE, result);
            eventManager.notify(EventManager.EventType.EMAIL, result);
        }
    }

    /**
     * 具体的业务逻辑：
     * 1. 抽奖
     * 2. 中奖后通知
     * @param uId
     * @return
     */
    public abstract LotteryResult draw(String uId);
}
